//
//  CpuMapManager.swift
//  Pods
//
//  Created by 100Years on 2025/6/10.
//

import UIKit

public class CpuMapManager: NSObject {
    static let shared = CpuMapManager()
    
    /// 数据来源 https://kylebing.cn/tools/iphone/
    ///         http://kylebing.cn/tools/iphone/
    ///
    private let deviceMap: [String: [String: String]] = [
        
        "iPhone 16 Pro Max" : ["CPU":"A18 Pro",
                               "Freq": "4.04GHz",
                               "Battery": "4685 mah"],
        "iPhone 16 Pro"     : ["CPU":"A18 Pro",
                               "Freq": "4.04GHz",
                               "Battery": "3582 mah"],
        "iPhone 16 Plus"    : ["CPU":"A18",
                               "Freq": "3.78GHz",
                               "Battery": "4674 mah"],
        "iPhone 16"         : ["CPU":"A18",
                               "Freq": "3.78GHz",
                               "Battery": "3561 mah"],
        
        "iPhone 15 Pro Max" : ["CPU":"A17 Pro",
                               "Freq": "3.77GHz",
                               "Battery": "4422 mah"],
        "iPhone 15 Pro"     : ["CPU":"A17 Pro",
                               "Freq": "3.77GHz",
                               "Battery": "3274 mah"],
        "iPhone 15 Plus"    : ["CPU":"A16",
                               "Freq": "3.46GHz",
                               "Battery": "4383 mah"],
        "iPhone 15"         : ["CPU":"A16",
                               "Freq": "3.46GHz",
                               "Battery": "3349 mah"],
        
        "iPhone 14 Pro Max" : ["CPU":"A16",
                               "Freq": "3.46GHz",
                               "Battery": "4323 mah"],
        "iPhone 14 Pro"     : ["CPU":"A16",
                               "Freq": "3.46GHz",
                               "Battery": "3200 mah"],
        "iPhone 14 Plus"    : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "4325 mah"],
        "iPhone 14"         : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "3279 mah"],
        
        "iPhone SE3"        : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "2018 mah"],
        "iPhone 13 Pro Max" : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "4352 mah"],
        "iPhone 13 Pro"     : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "3095 mah"],
        "iPhone 13"         : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "3227 mah"],
        "iPhone 13 Mini"    : ["CPU":"A15",
                               "Freq": "3.20GHz",
                               "Battery": "2406 mah"],
        
        "iPhone 12 Pro Max" : ["CPU":"A14",
                               "Freq": "3.00GHz",
                               "Battery": "3687 mah"],
        "iPhone 12 Pro"     : ["CPU":"A14",
                               "Freq": "3.00GHz",
                               "Battery": "2815 mah"],
        "iPhone 12"         : ["CPU":"A14",
                               "Freq": "3.00GHz",
                               "Battery": "2851 mah"],
        "iPhone 12 Mini"    : ["CPU":"A14",
                               "Freq": "3.00GHz",
                               "Battery": "2227 mah"],
        
        "iPhone SE2"        : ["CPU":"A13",
                               "Freq": "2.0GHz",
                               "Battery": "1821 mah"],
        
        "iPhone 11 Pro Max" : ["CPU":"A13",
                               "Freq": "2.0GHz",
                               "Battery": "3969 mah"],
        "iPhone 11 Pro"     : ["CPU":"A13",
                               "Freq": "2.0GHz",
                               "Battery": "3046 mah"],
        "iPhone 11"         : ["CPU":"A13",
                               "Freq": "2.0GHz",
                               "Battery": "3110 mah"],
        
        "iPhone XR"         : ["CPU":"A13",
                               "Freq": "2.0GHz",
                               "Battery": "2942 mah"],
        "iPhone XS Max"     : ["CPU":"A12",
                               "Freq": "2.49GHz",
                               "Battery": "3174 mah"],
        "iPhone XS"         : ["CPU":"A12",
                               "Freq": "2.49GHz",
                               "Battery": "2658 mah"],
        "iPhone X"          : ["CPU":"A11",
                               "Freq": "1.4GHz",
                               "Battery": "2716 mah"],
        
        "iPhone 8 Plus"     : ["CPU":"A11",
                               "Freq": "1.4GHz",
                               "Battery": "2691 mah"],
        "iPhone 8"          : ["CPU":"A11",
                               "Freq": "1.4GHz",
                               "Battery": "1821 mah"],
        "iPhone 7 Plus"     : ["CPU":"A11",
                               "Freq": "1.4GHz",
                               "Battery": "2900 mah"],
        "iPhone 7"          : ["CPU":"A10",
                               "Freq": "1.3GHz",
                               "Battery": "1960 mah"],
        "iPhone SE"         : ["CPU":"A9",
                               "Freq": "1.85GHz",
                               "Battery": "1624 mah"],
        "iPhone 6s Plus"    : ["CPU":"A9",
                               "Freq": "1.85GHz",
                               "Battery": "2750 mah"],
        "iPhone 6s"         : ["CPU":"A9",
                               "Freq": "1.85GHz",
                               "Battery": "1715 mah"],
    ]
    
    private(set) var modelName: String
    private(set) var cpuName: String?
    /// CPU频率
    private(set) var cpuFreq: String?
    private(set) var batteryCapacity: String?
    
    override init() {
        
        modelName = UIDevice.current.modelName
        cpuName = deviceMap[modelName]?["CPU"]
        cpuFreq = deviceMap[modelName]? ["Freq"]
        batteryCapacity = deviceMap[modelName]?["Battery"]
        
        super.init()
    }
}

extension UIDevice {
    
    var modelIdentifier: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { (identifier, element) in
            guard let value = element.value as? Int8, value != 0 else {
                return identifier
            }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        
        return identifier
    }
    
    /// 数据来源 https://www.jianshu.com/p/d9068fee295e
    public var modelName: String {
        
        let identifier = modelIdentifier
        
        switch identifier {
        case "iPhone8,1":
            return "iPhone 6s"
        case "iPhone8,2":
            return "iPhone 6s Plus"
        case "iPhone8,4":
            return "iPhone SE"
        case "iPhone9,1":
            return "iPhone 7"; //国行、日版、港行
        case "iPhone9,2":
            return "iPhone 7 Plus"; //国行、港行
        case "iPhone9,3":
            return "iPhone 7"; //美版、台版
        case "iPhone9,4":
            return "iPhone 7 Plus"; //美版、台版
        case "iPhone10,1":
            return "iPhone 8"; //国行(A1863)、日行(A1906)
        case "iPhone10,2":
            return "iPhone 8 Plus"; //国行(A1864)、日行(A1898)
        case "iPhone10,3":
            return "iPhone X"; //国行(A1865)、日行(A1902)
        case "iPhone10,4":
            return "iPhone 8"; //美版(Global/A1905)
        case "iPhone10,5":
            return "iPhone 8 Plus"; //美版(Global/A1897)
        case "iPhone10,6":
            return "iPhone X";//美版(Global/A1901)
            
        // 刘海屏(除SE外)
        case "iPhone11,8":
            return "iPhone XR"
        case "iPhone11,2":
            return "iPhone XS"
        case "iPhone11,6":
            return "iPhone XS Max"
        case "iPhone11,4":
            return "iPhone XS Max"
        case "iPhone12,1":
            return "iPhone 11"
        case "iPhone12,3":
            return "iPhone 11 Pro"
        case "iPhone12,5":
            return "iPhone 11 Pro Max"
        case "iPhone12,8":
            return "iPhone SE2"; //(2nd generation)
        case "iPhone13,1":
            return "iPhone 12 mini"
        case "iPhone13,2":
            return "iPhone 12"
        case "iPhone13,3":
            return "iPhone 12 Pro"
        case "iPhone13,4":
            return "iPhone 12 Pro Max"
        case "iPhone14,2":
            return "iPhone 13 Pro"
        case "iPhone14,3":
            return "iPhone 13 Pro Max"
        case "iPhone14,4":
            return "iPhone 13 mini"
        case "iPhone14,5":
            return "iPhone 13"
        case "iPhone14,6":
            return "iPhone SE3"; //(2nd generation)
        case "iPhone14,7":
            return "iPhone 14"
        case "iPhone14,8":
            return "iPhone 14 Plus"
            
        // 灵动岛
        case "iPhone15,2":
            return "iPhone 14 Pro"
        case "iPhone15,3":
            return "iPhone 14 Pro Max"
            
        case "iPhone16,2":
            return "iPhone 15 Pro Max"
        case "iPhone16,1":
            return "iPhone 15 Pro"
        case "iPhone15,5":
            return "iPhone 15 Plus"
        case "iPhone15,4":
            return "iPhone 15"

            
        case "iPhone17,3":
            return "iPhone 16"
        case "iPhone17,4":
            return "iPhone 16 Plus"
        case "iPhone17,1":
            return "iPhone 16 Pro"
        case "iPhone17,2":
            return "iPhone 16 Pro Max"
            
            
            
        default:
            return "--"
        }
    }
    
    var currentSysVersion: Int {
        let version = UIDevice.current.systemVersion
        let v1 = version.components(separatedBy: ".").first ?? "0"
        let doubleVersion = Double(v1)
        if let doubleVersion = doubleVersion {
            return Int(doubleVersion)
        }
        return 0
    }
    
    // 版本判断
    func currentSysVersionEqualOrGreater(than version: String) -> Bool {
        var sysValues = UIDevice.current.systemVersion.components(separatedBy: ".")
        var values = version.components(separatedBy: ".")
        
        // 位数不同，少的补0
        let count = max(sysValues.count, values.count)
        if sysValues.count < count {
            let more = Array(repeating: "0", count: count-sysValues.count)
            sysValues.append(contentsOf: more)
        }
        if values.count < count {
            let more = Array(repeating: "0", count: count-values.count)
            values.append(contentsOf: more)
        }
        
        for idx in 0..<count {
            if let sv = Int(sysValues.safeObj(At: idx) ?? "0"),
                let v = Int(values.safeObj(At: idx) ?? "0") {
                if sv > v {
                    return true
                } else if sv < v {
                    return false
                }
            }
        }
        return true
    }
    
    var iPhoneXids: [String] {
        return ["iPhone10,3", "iPhone10,6"]
    }
    
    var iPhoneSEids: [String] {
        return ["iPhone8,4", "iPhone12,8", "iPhone14,6"]
    }
}

// 传感器样式： 无 | 刘海 | 灵动岛
enum DeviceSensorStyle {
    case none
    case bang
    case island
}

extension UIDevice {
    func sensorStyle() -> DeviceSensorStyle {
#if targetEnvironment(simulator)
        return .island
#endif
        let identifier = UIDevice.current.modelIdentifier
        // SE
        if iPhoneSEids.contains(identifier) {
            return .none
        }
        // X
        if iPhoneXids.contains(identifier) {
            return .bang
        }
        if identifier.hasPrefix("iPhone") {
            let digital = identifier.replacingOccurrences(of: "iPhone", with: "")
            let arr = digital.components(separatedBy: ",")
            if let v1 = arr.safeObj(At: 0),
               let vv1 = Double(v1) {
                if vv1 >= 15 {
                    return .island
                } else {
                    if vv1 < 11 {
                        return .none
                    }
                    return .bang
                }
            }
        }
        return .none
    }
    
    // 刘海屏 及以后设备才可以升级到16.1
    var supportLiveActivity: Bool {
        guard #available(iOS 16.1, *) else {
            return false
        }
        let style = sensorStyle()
        return style != .none
    }
    
    var sensorSize: CGSize {
        let style = sensorStyle()
        switch style {
        case .none:
            return .zero
        case .bang:
            let ratio: CGFloat = UIScreen.main.bounds.width/375.0
            return CGSize(width: 219*ratio, height: 30*ratio)
        case .island:
            return CGSize(width: 125, height: 36.7)//16s 和 16sp 是 126*37.3
        }
    }
    
    var sensorTop:Double{
        let kScreenWidth = UIScreen.main.bounds.size.width
        let kScreenHeight = UIScreen.main.bounds.size.height
        //16s和 16sp
        if (kScreenWidth == 440 && kScreenHeight == 956) || (kScreenWidth == 402 && kScreenHeight == 874) ||  modelName.contains("iPhone 16 Pro") {
            return 14.0
        } else {
            return 11.3
        }
    }
}
